package cn.mxz.controller;

import cn.mxz.pojo.Orders;
import cn.mxz.pojo.ResultData;
import cn.mxz.service.OrderService;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author 小毛
 * @Date 2021/12/2 20:41
 * @Remark 佛祖保佑永无bug~
 */
@Controller
@CrossOrigin
@RequestMapping("alipay")
public class AliPayController {
    private final String APP_ID = "2021000118659825";
    private final String APP_PRIVATE_KEY = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCm/WJeKLpE9aRAVlDvtYFeYMjaN0/PSxQrS9KEt35wPwgU1DeqNM4ON9xqBO+8R8ird4iYFBDD3axEOIShZBRulkkOBOpW25+4ugXaZsIheopzIcZ4zTMt7x6x2Uuud0tpLTHJBPk4E/wWECLWoKJZ6K4Rilc1eeRf6vgY+hCZGeoIQ6xEGK+1xq4fgIdAQC1jqZ9HVoAB8nyGXjYX+wlW9yeTksAsfCUKYoOq746TtP2NWvlOQi5KVygdwe7uFe+mJ/cILWfo15g34HvMJW2Ah/NX+HUwiv+9+2xOK0uKPzK3AEjCVG5bttxoCtWyaMk+fTqNKYe86tjmfaSpm9RfAgMBAAECggEBAJEdcPZSfkyfCvW+DOfC24Ggqeq4VmrV29kkMvWSYBY7MFKpeRY4fTht4uVfFNaRqxkW72T5SvhrEuDWHKwDxJ21jBbavqpsgQiO37dDxUBCtT4IA+nGqMsqGYqEQviCQs5LsA/4enr5IwApVZ0eSvfG9D3Z8p9OJeAU2TUbWoEwVGHABn/Wb1ZBNpG5gov7zCpc1pGslA2ZJzHjnpBNZ2zbJRmSvUEe4t54MenZOEWNSZfGN/5v8ebFEm1ntXTETC/rAfYfLdi5quwltcOJeQzlauSjF8LvEHyaCq7cc441k6If0ZTdhdgLPoFht4MRuxFkJLsPPOA/v2LHD1xeqsECgYEA1T4LB7NOUGz9/4qBKdS1+FLVvrEWUS4kzrzeJf3tZkjpLyGk8bV4e0E0QfpUMttmT3A/5bv9hFGyieq/APEYbYD69IDg25ut0lcYLVtG3s8H/XSrIPGsCZRak0TRHsiVRCQ7kDIDczlB/e7utt3j8KRT05BPYQf9LEg+0r4zN9UCgYEAyHklugjNnMFIpG2VuxuAB6gJDt/Pmh9eQL4NYlMtETn8fC6BI+y6pXJSwR+Qz/wH/IUabiJJ6aBdl2REZm86RtP7RMMPQRLEIiAkLQdCCdP+EtOYt7hZdlgKnBSA9wUn5FN2kB+fEvyxik4+kPIuMMxdlr8OCrtqmfGJNjubyWMCgYBiGebtJfpTWbEIjOmbGvaURVd8IPAJbNcWHSF+DPHYapaMTkgZwsiou3iVGdr+gpzw0s0ynWemJgejF1HD1ybLydip/1hy743qBJNk2mciPdCJM9E+mK/ywENjGPez18eDz79vOPMJYUAQ2IkNq8sffBnsbKl4FDrO0nXEcigR+QKBgE1uRX0aY1hVa1pfHUMctxG9/GmrVoMf9IarIX5FxBVwQS4xxB19FQZMJtyC9arvnKITQwcg5U2AbEXKK4UpFug+fkGr2Zt3xRutMGvX7tDe+baJ1eW4JY3JKafpX3qJmkPdXOED5awuv+A8onvXnrGobcmI18Kbauw015uD4IMjAoGBAJPDO/QUmczwAKo4782QOyb+Mzfu6qLYzo7Dn0yDSMFpl+z54oZWQF1PM3zsyGzfN8zWwk4T9csDVOa2LFvqznYQW31nv8znY7UrOBQ04xVhL9JrPoFIO5yWxJyMRORNLjdWa8vGbkQAzf+8eYynXHacW4FkRbf+TaeoqVw1c3Rc";
    private final String CHARSET = "UTF-8";
    private final String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmmImGglhKkb+Jii+P9ub7yMwhcQ8hHh8JwFtiAfch92t/JMSF/dvr3wfzSgiTJscuV1DZNarTEQZVq0u5Zw77gQsGJsrWCZaaF7OcMdF+HJ2s7TUAy3bYRtYvWIBd4Zxy8LXaxfhy9g6Kl185xv41q3lG/Fs0ZYntKPSEvWgwpK+e2tpv4JiOg3vRE555WjFzc1PkpinyizRvFbVkt+8FzT3ovz9pEYwcWAuApZbGk0PahHshGLLVhpNIwSvBDNnGSAb8ajdGYpQHYfpUKl2GEHDcuCY46pRjeh2zSUfNYmt9Or4CjkXQSsu5YKGmZqQQmZ9HM3EGY24jwCrL2D5FwIDAQAB";
    //这是沙箱接口路径,正式路径为https://openapi.alipay.com/gateway.do
    private final String GATEWAY_URL ="https://openapi.alipaydev.com/gateway.do";
    private final String FORMAT = "JSON";
    //签名方式
    private final String SIGN_TYPE = "RSA2";
    //支付宝异步通知路径,付款完毕后会异步调用本项目的方法,必须为公网地址
    private final String NOTIFY_URL = "http://171.83.4.181:8888/notifyUrl";
    //支付宝同步通知路径,也就是当付款完毕后跳转本项目的页面,可以不是公网地址
    private final String RETURN_URL = "http://localhost:8080/my-order";

    @Autowired
    private OrderService orderService;

    @RequestMapping("{orderId}")
    public void alipay(@PathVariable("orderId") String orderId, HttpServletResponse httpResponse) throws IOException {
        //实例化客户端,填入所需参数
        AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY_URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        //在公共参数中设置回跳和通知地址
//        request.setNotifyUrl(NOTIFY_URL);
        request.setReturnUrl(RETURN_URL);
//        request.setNotifyUrl("http://171.83.4.181:8888/order/"+orderId);
        //根据订单编号,查询订单相关信息
        ResultData<Orders> resultData = orderService.selectByOrderId(orderId);
        Orders order = resultData.getData();
        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = orderId;
        //付款金额，必填
        String total_amount = order.getTotalAmount().toString();
        //订单名称，必填
        String subject = order.getUntitled();
        //调用方法将订单状态修改
        orderService.changeStatus(orderId);
        //商品描述，可空
        String body = "";
        request.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        String form = "";
        try {
            form = alipayClient.pageExecute(request).getBody(); // 调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        httpResponse.setContentType("text/html;charset=" + CHARSET);
        httpResponse.getWriter().write(form);// 直接将完整的表单html输出到页面
        httpResponse.getWriter().flush();
        httpResponse.getWriter().close();

    }

}
